package com.prs.crm.action.cdxh;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;

import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.cdxh.CrmCustomer;
import com.prs.crm.domain.cdxh.SaleOrder;
import com.prs.crm.domain.cdxh.cd_Product;

import com.prs.crm.service.cdxh.CrmCustomerService;
import com.prs.crm.service.cdxh.NewPlanOrderService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.cd_ProductService;

public class SaleOrderAction extends ReportAction {

	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private cd_ProductService cd_ProductService;
	private CrmCustomerService crmCustomerService;
	private SaleOrder saleOrder;
	private Collection<SaleOrderDetail> saleProducts;
	private Collection<SaleOrderDetail> saleProductsA;
	private Collection<SaleOrderDetail> slist = new ArrayList<SaleOrderDetail>();
	private SaleOrderService saleOrderService;
	private CrmCustomer crmcustomer;
	private NewPlanOrderService newPlanOrderService;
	private String[] productCodes;
	private String[] pronames;
	private String cusCode;
	private String[] productNorms;
	private String[] productColors;
	private String[] productPackges;
	private String[] productNums;
	private String[] productUnits;
	private String[] poductPrices;
	private String[] productSquares;
	private String[] prototalPrices;
	private String listString;
	private String planOrder;
	
	private Integer[] crmSaleIds;
	

	@Action("listSaleProductState")
	public String listSaleProductState() {
		saleProducts = saleOrder.getDetails();
		String planOrder = saleOrder.getSaleOrderCode();
		for (SaleOrderDetail sd : saleProducts) {
			sd.setSaleDates(this.getSaleOrderService().setSaleState(planOrder,
					sd.getProductCode()));
			// System.out.println("zhuangta"+sd.getSaleDates());
			slist.add(sd);
		}

		return SUCCESS;
	}
	
	@Clew(results = { "success", "none" })
	@Action(value = "confirmNumProduct", results = {
			@Result(name = "success", type = "redirect", location = "listProPlanNum"),
			@Result(name = "error", type = "chain", location = "listProPlanNum") })
	public String confirmNumProduct() {
		this.getSaleOrderService().setProPlanNums(saleOrder, productNums,productCodes);	
		this.setPrompt("计划生产数量处理中....");
		return SUCCESS;
	}
	
	@Action(value = "removeCrmSaleOrder", results = { @Result(name = "success", type = "redirect", location = "listSaleOrder") })
	public String removeCrmSaleOrder() {
		if(crmSaleIds==null){
		
		}else{
		this.getSaleOrderService().setSaleOrders(crmSaleIds);
		this.setPrompt("提交成功");
		}
		return SUCCESS;
	}

	
	@Clew(results = { "success", "none" })
	@Action(value = "saveSaleOrder", results = {
			@Result(name = "success", type = "redirect", location = "listSaleCustomer"),
			@Result(name = "error", type = "chain", location = "regSaleOrder") })
	public String saveSaleCustomer() {
		if (productCodes == null || productCodes.length == 0) {
			this.addActionError("商品不能为空,请添加商品！");
			return ERROR;
		}
		if (productPackges == null || productPackges.length == 0) {
			this.addActionError("商品包装方式不能为空！");
			return ERROR;
		}
		int i = 1;
		for (String s : productPackges) {
			if (s.equals("") || s == null) {
				this.addActionError("商品包装方式第" + i + "行为空！");
				return ERROR;
			}
			i++;
		}
		if (isnull(saleOrder.getCusTel())) {
			if (isphone(saleOrder.getCusTel())) {
				this.addActionError("电话号码格式有错!请重新输入");
				return ERROR;
			}
		}
		saleOrder.setRushOrder("否");
		this.getSaleOrderService().saveRegister(saleOrder, productCodes,
				pronames, productNorms, productColors, productPackges,
				productNums, productUnits, poductPrices, productSquares,
				prototalPrices);
		this.setPrompt("订单登记成功");
		return SUCCESS;
	}

	@Action("regSaleOrder")
	public String register() {
		if (saleOrder == null) {
			saleOrder = new SaleOrder();
			saleOrder.setCusStyle(crmcustomer.getCusStyle());
			saleOrder.setCusName(crmcustomer.getCusName());
			saleOrder.setCusCode(crmcustomer.getCusCode());
			saleOrder.setSaleNum(this.getSaleOrderService().getsaleNum());
		}
		return SUCCESS;
	}

	@Action("editSaleState")
	public String editSaleState() {
		// System.out.println("对象=" + saleOrder);
		saleOrder = this.getSaleOrderService().getSaleOrder(saleOrder.getId());
		// System.out.println("之后的对象=" + saleOrder);
		planOrder = saleOrder.getSaleNum();
		// System.out.println("订单制=" + saleOrder.getSaleOrderCode());

		return SUCCESS;
	}

	@Action(value = "saveSaleState", results = { @Result(name = "success", type = "chain", location = "editSaleState"), })
	public String saveState() {
		String state = saleOrder.getRushOrder();
		saleOrder = getSaleOrderService().get(saleOrder.getId());
		saleOrder.setRushOrder(state);
		this.getSaleOrderService().save(saleOrder);
		this.getSaleOrderService().setAllRushOrder(saleOrder);
		return SUCCESS;
	}

	@Action("listSaleProduct")
	public String selectcdProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);

		criteria.add(Restrictions.or(Restrictions.eq("customer_number", ""),
				Restrictions.isNull("customer_number")));
		pagination = this.getPaginationHelper().prepare(pagination);

		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listPrivateProduct")
	public String listPrivateProduct() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		criteria.add(Restrictions.eq("customer_number", this.getCusCode()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listSaleOrder")
	public String listSelectOrder() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SaleOrder.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value="exportOrderReport", results = { @Result(name = "success"
		,type="stream",params={"contentType","${contentType}","inputName","inputStream"
		,"bufferSize","1024","contentDisposition","filename=${outfileName}"}) })
	public String reportOrder()
	{
		/*if(crmSaleIds==null){
			
		}else{*/
		
		
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		ReportTable table = new ReportTable("订单");
		table.addCell(new ReportCell("日期","recordTime","yyyy-MM-dd"));
		table.addCell(new ReportCell("订单号","salesCodeN"));
		table.addCell(new ReportCell("客户名称","productName"));
		table.addCell(new ReportCell("颜色","productColor"));
		table.addCell(new ReportCell("规格","productNorm"));
		table.addCell(new ReportCell("数量","productNum"));
		table.addCell(new ReportCell("包装方式","productPaceStyle"));
		table.addCell(new ReportCell("备注","remark",Constrains.getReportOrder()));
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(SaleOrderDetail.class);
		this.setOutFilePath(this.getReportService().reportSaleOrder(criteria, table, new ExportConfig(),this.getExportType()));
		//}
		return SUCCESS;
		
	}
	
	@Action("listProPlanNum")
	public String listProPlanNum() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SaleOrder.class);
		criteria.add(Restrictions.lt("proPlanNums", 5));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listSaleCustomer")
	public String listSaleCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CrmCustomer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewSaleOrder")
	public String viewSaleOrder() {

		saleOrder = this.getSaleOrderService().get(saleOrder.getId());
		this.saleProducts = saleOrder.getDetails();
		return SUCCESS;
	}

	@Action("viewConfirmNum")
	public String viewConfirmNum() {
		saleOrder = this.getSaleOrderService().get(saleOrder.getId());
		this.saleProducts = saleOrder.getDetails();
		for (SaleOrderDetail dt : saleProducts) {
			cd_Product cp=this.getCd_ProductService().getProductObject(dt.getProductCode());
			if(cp!=null){
				dt.setInventoryNum(cp.getStockNums());
			}else{
				dt.setInventoryNum(0);
			}		
			slist.add(dt);
		}
		return SUCCESS;
	}
	
	////////////////////////////////////////////////////////////////////////////////////
	@Action("listPrivateProduct1")
	public String listPrivateProduct1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);
		criteria.add(Restrictions.eq("customer_number", this.getCusCode()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	
	
	@Action("listSaleProduct1")
	public String selectcdProduct1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				cd_Product.class);

		criteria.add(Restrictions.or(Restrictions.eq("customer_number", ""),
				Restrictions.isNull("customer_number")));
		pagination = this.getPaginationHelper().prepare(pagination);

		this.setPagination(this.getNewPlanOrderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("query_list")
	public String query_list() {
		return SUCCESS;
	}
	@Action("query_list1")
	public String query_list1() {
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public CrmCustomerService getCrmCustomerService() {
		return crmCustomerService;
	}

	public void setCrmCustomerService(CrmCustomerService crmCustomerService) {
		this.crmCustomerService = crmCustomerService;
	}

	public Collection<SaleOrderDetail> getSaleProducts() {
		return saleProducts;
	}

	public void setSaleProducts(Collection<SaleOrderDetail> saleProducts) {
		this.saleProducts = saleProducts;
	}

	public CrmCustomer getCrmcustomer() {
		return crmcustomer;
	}

	public void setCrmcustomer(CrmCustomer crmcustomer) {
		this.crmcustomer = crmcustomer;
	}

	public NewPlanOrderService getNewPlanOrderService() {
		return newPlanOrderService;
	}

	public void setNewPlanOrderService(NewPlanOrderService newPlanOrderService) {
		this.newPlanOrderService = newPlanOrderService;
	}

	public String[] getProductCodes() {
		return productCodes;
	}

	public void setProductCodes(String[] productCodes) {
		this.productCodes = productCodes;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public String[] getPronames() {
		return pronames;
	}

	public void setPronames(String[] pronames) {
		this.pronames = pronames;
	}

	public String getCusCode() {
		return cusCode;
	}

	public void setCusCode(String cusCode) {
		this.cusCode = cusCode;
	}

	public SaleOrder getSaleOrder() {
		return saleOrder;
	}

	public void setSaleOrder(SaleOrder saleOrder) {
		this.saleOrder = saleOrder;
	}

	public String[] getProductNorms() {
		return productNorms;
	}

	public void setProductNorms(String[] productNorms) {
		this.productNorms = productNorms;
	}

	public String[] getProductColors() {
		return productColors;
	}

	public void setProductColors(String[] productColors) {
		this.productColors = productColors;
	}

	public String[] getProductPackges() {
		return productPackges;
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

	public void setProductPackges(String[] productPackges) {
		this.productPackges = productPackges;
	}

	public String[] getProductNums() {
		return productNums;
	}

	public void setProductNums(String[] productNums) {
		this.productNums = productNums;
	}

	public String[] getProductUnits() {
		return productUnits;
	}

	public void setProductUnits(String[] productUnits) {
		this.productUnits = productUnits;
	}

	public String[] getPoductPrices() {
		return poductPrices;
	}

	public void setPoductPrices(String[] poductPrices) {
		this.poductPrices = poductPrices;
	}

	public String[] getProductSquares() {
		return productSquares;
	}

	public void setProductSquares(String[] productSquares) {
		this.productSquares = productSquares;
	}

	public String[] getPrototalPrices() {
		return prototalPrices;
	}

	public void setPrototalPrices(String[] prototalPrices) {
		this.prototalPrices = prototalPrices;
	}

	public boolean isphone(String str) {
		boolean tag = false;
		final String pattern = "^(([0-9]{3,4})|[0-9]{3,4}-)?[0-9]{7,8}$";
		final Pattern patt = Pattern.compile(pattern);
		final Matcher mat2 = patt.matcher(str);
		if (!mat2.find()) {
			tag = true;
		}
		return tag;
	}

	public boolean isnull(String str) {
		boolean s = true;
		if (str.equals("") || str == null) {
			s = false;
		}
		return s;
	}

	public String getPlanOrder() {
		return planOrder;
	}

	public void setPlanOrder(String planOrder) {
		this.planOrder = planOrder;
	}

	public Collection<SaleOrderDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<SaleOrderDetail> slist) {
		this.slist = slist;
	}

	public Collection<SaleOrderDetail> getSaleProductsA() {
		return saleProductsA;
	}

	public void setSaleProductsA(Collection<SaleOrderDetail> saleProductsA) {
		this.saleProductsA = saleProductsA;
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}

	public Integer[] getCrmSaleIds() {
		return crmSaleIds;
	}

	public void setCrmSaleIds(Integer[] crmSaleIds) {
		this.crmSaleIds = crmSaleIds;
	}

}
